package com.hj.ipproxy.handler;

import java.util.List;
import java.util.Set;

import com.hj.ipproxy.bean.HttpEntity;
import com.hj.ipproxy.bean.ProxyIp;
import com.hj.ipproxy.service.ProxyIpServiceImpl;
import com.hj.tools.StringUtil;
/**
 * http://www.kuaidaili.com
 * @author HJ
 */
public class KuaidailiComHandler extends FetchIpHandler {

	@Override
	protected void initHttpEntity(HttpEntity ht) {
	}

	@Override
	protected String[] getUrls() {
		int len = 1;
		String[] urls = new String[len];
		for (int i=1; i<=len; i++) {
			urls[i-1] = "http://www.kuaidaili.com/free/inha/"+i+"/";
		}
		return urls;
	}

	@Override
	protected List<String[]> getProxyIps(HttpEntity ht) {
		List<String[]> list = null;
		if (ht.send()) {
			try {
				list = StringUtil.matchAll(ht.getBody(), "<td>(.+?)</td>");
				Thread.sleep(1800);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	protected void doParse(Set<ProxyIp> iplist, List<String[]> list, ProxyIpServiceImpl proxyIpSev) {
		System.out.println(Thread.currentThread().getName()+"KuaidailiComHandler总共抓取了("+list.size()+")条IP");
		try {
			for (int i = 0; i < list.size(); i=i+7) {
				String ip = list.get(i)[1];
				String portStr = list.get(i+1)[1];
				int port = Integer.parseInt(portStr);
				String type = list.get(i+3)[1];
				ProxyIp pi = new ProxyIp();
				pi.setIp(ip);
				pi.setPort(port);
				pi.setType(type);
				checkAndFilterProxyIpInfo(pi, iplist, proxyIpSev);
			}
		} catch (Exception e) {
			System.out.println("抓取代理IP失败");
		}
	}
}
